package com.whfc.ms.api.emp.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppEmpCorpDTO;
import com.whfc.emp.dto.AppGroupDTO;
import com.whfc.emp.param.AppEmpGroupAddParam;
import com.whfc.emp.param.AppEmpGroupEditParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author sun_guodong
 * @description 人员班组接口
 * @date 2020-08-01
 */
//@RequiresPermissions("emp/group")
@RestController
@RequestMapping("/ms/api/group")
public class AppGroupController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAppGroupService msAppGroupService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("projectId") Integer projectId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "corpId", required = false) Integer corpId) {
        logger.info("班组列表,loginer:{},projectId:{},pageNum:{},pageSize:{},keyword:{},corpId:{}",
                user.getUsername(), projectId, pageNum, pageSize, keyword, corpId);
        PageData<AppGroupDTO> pageData = msAppGroupService.list(projectId, pageNum, pageSize, keyword, corpId);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/groupListByCorpId")
    public Result groupListByCorpId(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("corpId") Integer corpId) {
        logger.info("根据合作单位查找班组列表,loginer:{},corpId:{}", user.getUsername(), corpId);
        ListData<AppGroupDTO> listData = msAppGroupService.groupListByCorpId(corpId);
        return ResultUtil.success(listData);
    }

    @GetMapping("/groupListByProjectId")
    public Result groupListByProjectId(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                       @RequestParam("projectId") Integer projectId) {
        logger.info("根据项目查找班组列表,loginer:{},projectId:{}", user.getUsername(), projectId);
        ListData<AppGroupDTO> listData = msAppGroupService.groupListByProjectId(projectId);
        return ResultUtil.success(listData);
    }

    @GetMapping("/groupListByDeptId")
    public Result groupListByDeptId(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("deptId") Integer deptId,
                                    @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("根据组织机构查找班组列表,loginer:{},deptId:{},keyword:{}", user.getUsername(), deptId, keyword);
        ListData<AppEmpCorpDTO> listData = msAppGroupService.groupListByDeptId(deptId, keyword);
        return ResultUtil.success(listData);
    }

    @RequiresPermissions("emp/group/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppEmpGroupAddParam param) {
        logger.info("添加班组,loginer:{},param:{}", user.getUsername(), param.toString());
        msAppGroupService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("emp/group/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @Validated @RequestBody AppEmpGroupEditParam param) {
        logger.info("编辑班组,loginer:{},param:{}", user.getUsername(), param.toString());
        msAppGroupService.edit(param);
        return ResultUtil.success();
    }

    @PostMapping("/anon/syncGroup")
    public Result edit(@Validated @RequestBody AppEmpGroupEditParam param) {
        logger.info("同步班组,param:{}",  param.toString());
        msAppGroupService.syncGroup(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("emp/group/delete")
    @PostMapping("/del/{groupId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("groupId") Integer groupId) {
        logger.info("删除班组,loginer:{},groupId:{}", user.getUsername(), groupId);
        msAppGroupService.del(groupId);
        return ResultUtil.success();
    }

    @PostMapping("/syncCropName")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam Integer deptId) {
        logger.info("班组列表--同步合作单位,loginer:{},deptId:{}", user.getUsername(), deptId);
        msAppGroupService.syncCropName(deptId);
        return ResultUtil.success();
    }

}
